package Com.Ring.TradingMobile;

import Com.Ring.TradingMobile.Lib.RET_CODE;
import Com.Ring.TradingMobile.Lib.Session;
import Com.Ring.TradingMobile.Service.WebService;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import Com.Ring.TradingMobile.Lib.Common;

public class DangNhapActivity extends Activity {
	public EditText txtUsername;
	public EditText txtPassword;
	public Button btn_Login;
	public Button btn_clear;
	//public TextView lb_Alarm;
	public Button btnBack;
	private static final int DIALOG_LOADING = 0;
	private static String TAG = "DangNhap_TagLog";
	
	private class LoginAsyncTask extends AsyncTask<String, Void, Integer>{
		
		Activity activity;
		public LoginAsyncTask(Activity act) {
			activity = act;
		}
		
		@Override
		protected Integer doInBackground(String... params) {
			activity.showDialog(DIALOG_LOADING);
			int LoginRetCode = WebService.Login(params[0], params[1]);
			return LoginRetCode;
		}

		@Override
		protected void onPostExecute(Integer LoginRetCode) {
			RET_CODE code = Com.Ring.TradingMobile.Lib.Common.GetRetCode(LoginRetCode);
			switch (code) {
			case SUCCESS:
				Session.IsLogin = true;
				Session.custAccount = txtUsername.getText().toString();
				Session.currentSubAccount = txtUsername.getText().toString();	
				//lb_Alarm.setText("");
				txtUsername.setText("");
				txtPassword.setText("");
				activity.removeDialog(DIALOG_LOADING);
				Intent home = new Intent(getBaseContext(), TradingMobileLoggedActivity.class);
				startActivity(home);
				break;
			default:
				Session.IsLogin = false;
				activity.removeDialog(DIALOG_LOADING);
				//lb_Alarm.setText(Common.GetRetCodeErrorMsg(LoginRetCode));
				Toast.makeText(DangNhapActivity.this,Common.GetRetCodeErrorMsg(LoginRetCode) , Toast.LENGTH_SHORT).show();
				break;
			}
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			
		}

		@Override
		protected void onProgressUpdate(Void... values) {

			super.onProgressUpdate(values);
		}
	}
	LoginAsyncTask loginAsyncTask;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_LOADING : {
				ProgressDialog dialog = new ProgressDialog(this);
				dialog.setMessage("Loading...");
				dialog.setIndeterminate(true);
				dialog.setCancelable(true);
				return dialog;
			}
		}
		return null;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dangnhap);
		Log.d(TAG, "onCreate");
		
		//Khoi tao doi tuong.
		txtUsername = (EditText)findViewById(R.id.txt_Username);				
		txtPassword = (EditText)findViewById(R.id.txt_Password);
		btn_Login = (Button)findViewById(R.id.btn_Login);
		btn_clear = (Button)findViewById(R.id.btn_Clear);
		//lb_Alarm = (TextView)findViewById(R.id.lb_Alarm);
		btnBack = (Button)findViewById(R.id.btnBack);
		
		txtUsername.setText("075C000011");
		txtPassword.setText("12345678");
		
		btnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(loginAsyncTask!=null) {
					loginAsyncTask.cancel(true);
					loginAsyncTask=null;
				}
				finish();
			}
		});
		
		btn_Login.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
	
				if(txtUsername.getText().toString().equals(""))
				{
					//lb_Alarm.setText(R.string.strAlarm_User_Empty);
					Toast.makeText(DangNhapActivity.this, R.string.strAlarm_User_Empty, Toast.LENGTH_SHORT).show();
					return;
				}
				
				if(txtPassword.getText().toString().equals(""))
				{
					//lb_Alarm.setText(R.string.strAlarm_Pass_Empty);
					Toast.makeText(DangNhapActivity.this, R.string.strAlarm_User_Empty, Toast.LENGTH_SHORT).show();
					return;
				}
				//lb_Alarm.setText("");
				showDialog(DIALOG_LOADING);
				if(loginAsyncTask!=null && loginAsyncTask.isCancelled()){
					loginAsyncTask.cancel(true);
					loginAsyncTask=null;
				}
				loginAsyncTask = (LoginAsyncTask)new LoginAsyncTask(DangNhapActivity.this).execute(txtUsername.getText().toString(),txtPassword.getText().toString());
				
			}
		});
		
		btn_clear.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
	
				txtUsername.setText("");
				txtPassword.setText("");
				txtUsername.setFocusable(true);
				//lb_Alarm.setText("");
			}
		});
	}
	@Override
	protected void onPause() {
		Log.d(TAG, "onPause");
		super.onPause();
	}
	@Override
	protected void onRestart() {
		Log.d(TAG, "onRestart");
		super.onRestart();
	}
	@Override
	protected void onResume() {
		Log.d(TAG, "onResume");
		if(Session.IsLogin)
		{
			Intent home = new Intent(getBaseContext(), TradingMobileLoggedActivity.class);
			startActivity(home);
			finish();
		}
		super.onResume();
	}
	@Override
	protected void onStart() {
		Log.d(TAG, "OnStart");
		super.onStart();
	}
	@Override
	protected void onStop() {
		Log.d(TAG, "OnStop");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "onDestroy");
		super.onDestroy();
		if(loginAsyncTask!=null) {
			loginAsyncTask.cancel(true);
			loginAsyncTask = null;
		}
	}
	
	/*public void ShowDialog() {
		this.showDialog(DIALOG_LOADING);
	}
	public void RemoveDialog() {
		this.removeDialog(DIALOG_LOADING);
	}*/
	
}
